// pages/chat/private/index.js
//获取应用实例
const app = getApp()
const recorderManager = wx.getRecorderManager()
const innerAudioContext = wx.createInnerAudioContext()
var tempFilePath;
Page({
  data: {
    time: '2018-8-28 9:30',
    chatList: [{
      id: 'other',
      chatType: 't',
      content: '欢迎来到和力分享课堂欢迎来到和力分享课堂欢迎来到和力分享课堂欢迎来到和力分享课堂欢迎来到和力分享课堂欢迎来到和力分享课堂'
    }, {
      id: 'me',
      chatType: 'i',
      content: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535532541076&di=1c001973eb3ac46321a24790f8cb3de6&imgtype=0&src=http%3A%2F%2Fimg3.cache.netease.com%2Fphoto%2F0026%2F2017-04-14%2FCI0B4RFF78LH0026.jpg'
    }, {
      id: 'me',
      chatType: 't',
      content: '美不美看大腿，就是不给你看~~~美不美看大腿，就是不给你看美不美看大腿，就是不给你看美不美看大腿，就是不给你看美不美看大腿，就是不给你看美不美看大腿，就是不给你看美不美看大腿，就是不给你看美不美看大腿，就是不给你看美不美看大腿，就是不给你看'
    }, {
      id: 'other',
      chatType: 't',
      content: '滚！！！'
    }, {
      id: 'me',
      chatType: 'i',
      content: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1535532541074&di=5192bdf0a00f28bf367a5812cd2ca11f&imgtype=0&src=http%3A%2F%2Fimg18.3lian.com%2Fd%2Ffile%2F201710%2F25%2Fd35b1dbf17aaeabbbad9567c981e3909.jpg'
    }, {
      id: 'me',
      chatType: 't',
      content: '叔叔，我们不约'
    },],
    autoIsShwo: false, //语音输入框是否显示
    autoButClo: false, //语音输入框背景颜色是否改变
    focus: false, //是否键盘聚焦
    screenHeight: 0, //屏幕高度
    value: '', //文本输入框值
    addPng: true, //加号图片是否显示
    botShow: false, //底部隐藏的功能列表是否显示
    bottom: 0, //底部输入框bottom单位
    hidbottom: 100, //点击加号事，屏幕向上滚动单位
  },
  //开始录音的时候
  start: function () {
    this.setData({
      autoButClo: true
    })
    const options = {
      duration: 60000, //指定录音的时长，单位 ms
      sampleRate: 16000, //采样率
      numberOfChannels: 1, //录音通道数
      encodeBitRate: 96000, //编码码率
      format: 'mp3', //音频格式，有效值 aac/mp3
      frameSize: 50, //指定帧大小，单位 KB
      audioSource: 'auto', //自动设置，默认使用手机麦克风，插上耳麦后自动切换使用耳机麦克风
    }
    //开始录音
    recorderManager.start(options);
    recorderManager.onStart(() => {
      console.log('recorder start')
    });
    //错误回调
    recorderManager.onError((res) => {
      console.log(res);
    })
  },
  //停止录音
  stop: function () {
    this.setData({
      autoButClo: false
    })
    recorderManager.stop();
    recorderManager.onStop((res) => {
      this.tempFilePath = res.tempFilePath;
      console.log('停止录音', res.tempFilePath)
      const {
        tempFilePath
      } = res
    })
  },
  //播放声音
  play: function () {
    innerAudioContext.autoplay = true
    innerAudioContext.src = this.tempFilePath,
      innerAudioContext.onPlay(() => {
        console.log('开始播放')
      })
    innerAudioContext.onError((res) => {
      console.log(res.errMsg)
      console.log(res.errCode)
    })
  },
  onLoad: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          screenHeight: res.screenHeight
        })
        wx.pageScrollTo({
          scrollTop: that.data.screenHeight,
        })
      }
    })

    // var timeOut = setTimeout(function(){
    //   that.handleTap2()
    // },5000)
  },
  /**
   * 点击语音图标，输入框隐藏，显示语音输入
   */
  autoTap: function () {
    var that = this
    if (that.data.autoIsShwo) {
      that.setData({
        focus: true,
      })
    }
    that.setData({
      autoIsShwo: !that.data.autoIsShwo,
      botShow: false
    })
    if (that.data.autoIsShwo) {
      that.setData({
        bottom: 0,
        hidbottom: 100
      })
    }
  },
  /**
   * 输入框输入事件
   */
  inputTap: function (e) {
    var that = this
    var value = e.detail.value
    var cursor = e.detail.cursor
    if (cursor > 0) {
      that.setData({
        addPng: false,
      })
    }
    that.setData({
      value,
    })
    if (that.data.value.length == 0) {
      that.setData({
        addPng: true
      })
    }
  },
  /**
   * 点击发送按钮事件
   */
  sendTap: function (e) {
    var that = this
    that.setData({
      value: '',
      addPng: true
    })
  },
  /**
   * 点击加号事件
   */
  sendImgTap: function () {
    var that = this
    // that.setData({
    //   botShow: !that.data.botShow,
    // })
    // if (that.data.botShow) {
    //   that.setData({
    //     focus: false
    //   })
    // }else{
    //   that.setData({
    //     focus: true
    //   })
    // }
    if (!that.data.focus && !that.data.botShow) {
      that.setData({
        focus: false,
        botShow: true
      })
    } else if (!that.data.focus && that.data.botShow) {
      that.setData({
        focus: true,
        botShow: false
      })
    } else if (that.data.focus && that.data.botShow) {
      that.setData({
        focus: false,
        botShow: true
      })
    } else if (that.data.focus && !that.data.botShow) {
      that.setData({
        focus: false,
        botShow: true
      })
    }

    var hidbottom = that.data.botShow ? 660 : 100
    var bottom = that.data.botShow ? 580 : 0
    that.setData({
      hidbottom: hidbottom,
      bottom: bottom
    })
  },
  /**
   * 输入框焦点触发事件
   */
  focusTap: function (e) {
    console.log(e)
    var that = this
    // that.setData({
    //   focus: true,
    // })
  },
  /**
   * 输入框失去焦点事件
   */
  blurTap: function (e) {
    console.log(e)
    var that = this
    that.setData({
      focus: false,
      botShow: false,
      bottom: 0,
      hidbottom: 100
    })
  },
  /**
   * 拉起键盘
   */
  getKeyTap: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        console.log(res)
      }
    })
    if (that.data.botShow) {
      that.setData({
        bottom: 580,
      })
    } else {
      that.setData({
        bottom: 0
      })
    }
    that.setData({
      focus: true,
      botShow: false,
    })
  },
  /**
   * 点击输入框以外的区域收起键盘
   */
  pushKeyTap: function () {
    var that = this
    that.setData({
      focus: false,
      botShow: false,
      bottom: 0,
      hidbottom: 100
    })
  },

  handleTap1: function (e) {
    console.log(e)
  },
  handleTap2: function (e) {
    console.log(e)
  },
  bindTextAreaBlur: function (e) {
    console.log("======")
    console.log(e)
  }
})